import { getConsumeList,getRechargeLogList } from '../../../api/api.js';
import { ASSETS } from '../../../config.js';
const app = getApp();
const os = app.os;

Page({
  data: {
    assets: ASSETS,
    imId: getApp().globalData.imId,
    current: 0,
    circular: false,
    duration: 500,
    rLoading: false,
    uLoading: false,
    currentPage: 1,
    pageTotal: 1,
    rCurrentPage: 1,
    rPageTotal: 1,
    limit: 10,
    windowHeight: 745,
    rechargeData: [],
    useData: [],
  },
  onLoad: function (options) {
    const _this = this;
    app.checkNoPayOrder(function(){
      _this.getRechargeLogList();
      _this.getConsumeList();
    });
  },

  onReady: function () {
    try {
      const res = os.getWindowInfo();
      this.setData({
        windowHeight: res.windowHeight
      });
    } catch (err) {

    }
    app.sendAppLogs('app', 3);
  },

  onShow: function () {
    this.setData({
      imId: getApp().globalData.imId,
    })
  },

  onSwiperChange: function (e) {
    this.setData({current: parseInt(e.detail.current)});
  },

  getConsumeList: function () {
    this.setData({uLoading: true});
    getConsumeList({
      page: this.data.currentPage,
      limit: this.data.limit,
    }).then(res => {
      let dataList = this.data.useData;
      let uLoading = false;
      if (res.code == 200) {
        if(res.data.current_page == 1){
          dataList = [];
        }
        if(res.data.list.length > 0){
          dataList = dataList.concat(res.data.list);
        }
        if(res.data.current_page < res.data.total_page && res.data.count > 0){
          uLoading = true;
        }
        this.setData({
          useData: dataList, 
          currentPage: parseInt(res.data.current_page), 
          pageTotal: res.data.total_page,
          uLoading: uLoading
        });
      }else{
        this.setData({uLoading: false});
        return app.Tips({title: res.msg});
      }
    }).catch(err => {
      this.setData({uLoading: false});
    });
  },

  getRechargeLogList: function () {
    this.setData({rLoading: true});
    getRechargeLogList({
      page: this.data.rCurrentPage,
      limit: this.data.limit,
    }).then(res => {
      let dataList = this.data.rechargeData;
      let rLoading = false;
      if (res.code == 200) {
        if(res.data.current_page == 1){
          dataList = [];
        }
        if(res.data.list.length > 0){
          dataList = dataList.concat(res.data.list);
        }
        if(res.data.current_page < res.data.total_page && res.data.count > 0){
          rLoading = true;
        }
        this.setData({
          rechargeData: dataList, 
          rCurrentPage: parseInt(res.data.current_page), 
          rPageTotal: res.data.total_page,
          rLoading: rLoading
        });
      }else{
        this.setData({rLoading: false});
        return app.Tips({title: res.msg});
      }
    }).catch(err => {
      this.setData({rLoading: false});
    });
  },

  onLoadRechargeData: function () {
    let currentPage = this.data.rCurrentPage + 1;
    if (currentPage <= this.data.rPageTotal){
      this.setData({rCurrentPage: currentPage});
      this.getRechargeLogList();
    }
  },

  onLoadUseData: function () {
    let currentPage = this.data.currentPage + 1;
    if (currentPage <= this.data.pageTotal){
      this.setData({currentPage: currentPage});
      this.getConsumeList();
    }
  },

  onScroll: function (e) {
    this.onSlideEvent(e);
  },

  /**
   * 滑动事件
   */
  onSlideEvent: function(e) {
    if (this.scrollHeight === undefined) {
      this.scrollHeight = [0, 0];
    }
    
    this.scrollHeight[1] = parseInt(e.detail.scrollTop);

    if (this.slideTimeId) {
      clearTimeout(this.slideTimeId);
    }
    
    this.slideTimeId = setTimeout(() => {
      clearTimeout(this.slideTimeId);

      if(this.scrollHeight[0] !== this.scrollHeight[1]){
        app.sendAppLogs('app', 5, {
          method_mark: this.scrollHeight[0],
          method_mark_1: this.scrollHeight[1],
        });
        this.scrollHeight[0] = this.scrollHeight[1];
      }

    }, 300);
  },

  onPlatformImError(e){
    app.Tips({
      title: "即将开通客服，请耐心等待"
    });
  },

  onPlatformImSucess: function(e){
    app.sendAppLogs('app', 4, {
      method_mark: 'im',
      method_mark_1: '官方客服'
    });
  },

  /**
   * 跳到播放页
   */
  toVideoPage: function(e) {
    const { item,type } = e.currentTarget.dataset;
    if (type === 1) {
      if (item?.gear_type !== 3) {
        return;
      }
      app.goPlayVideoPage(item.playlet_id);
    }

    if (type === 2) {
      app.goPlayVideoPage(item.playlet_id, item.chapter_id);
    }
  },

  onOption: function (e) {
    this.setData({current: parseInt(e.currentTarget.dataset.id)});
  }
})